/* eslint-disable no-const-assign */
/* eslint-disable no-unused-vars */
<template>
  <van-button :icon="attitude ? 'good-job':'good-job-o'"
    :class="{
    liked:attitude===1
  }"
    :loading="loading"
    @click="onCollect"></van-button>
</template>

<script>

import { addLike, deleteLike } from '@/api/user.js'
export default {
  name: 'LikeArticle',
  model: {
    prop: 'attitude',
    event: 'updata-attitude'
  },
  props: {
    // 控制点赞参数
    attitude: {
      type: Number,
      required: true
    },
    // 用户点赞的id
    articleId: {
      type: [String, Number, Object],
      required: true
    }
  },
  data () {
    return {
      loading: false
    }
  },
  methods: {
    async onCollect () {
      console.log(11)
      this.loading = true
      try {
        // 点赞状态 -1 为点赞 1点赞成功

        let status = -1
        if (this.attitude === 1) {
          // 已点赞 取消点赞
          await deleteLike(this.articleId)
        } else {
          await addLike(this.articleId)
          // eslint-disable-next-line no-const-assign
          status = 1
        }

        // 更新视图
        this.$emit('updata-attitude', status)
      } catch (err) {
        this.$toast.fail('操作失败，请重试！')
      }

      this.loading = false
    }
  }
}
</script>

<style lang="less" scoped>
.liked {
  .van-icon {
    color: #e5645f;
  }
}
</style>
